// pages/device/device.js

const config = require('../../config.js');
const app = getApp();

function getMyDeviceList(that) {
  var token = app.globalData.token;
  var openid = app.globalData.openid;
  wx.showToast({
    title: '正在获取我的设备...',
    icon: 'loading'
  });
  wx.request({
    //后台接口地址
    url: config.serverUrl + '/web/api/inner/opr/getMyDeviceList',
    data: {
      openid: openid,
      token: token
    },
    method: 'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    success: function (res) {
      wx.hideToast();
      if (res) {
        var respData = res.data;
        if (respData.code == '000000') {
          console.log(respData.list);
          that.setData({
            list: respData.list
          })
        } else if (respData.code == '000003') {
          wx.showToast({
            title: '请退出小程序再登录!',
            icon: 'none',
            duration: 5000
          })
        } else {
          console.log("**bindCarlafail**");
          console.log(res)
          console.log("**bindCarla fail**");
          wx.showToast({
            title: '获取我的设备失败!',
            icon: 'none',
            duration: 5000
          })
        }
      } else {
        console.log("**bindCarla fail**");
        console.log(res)
        console.log("**bindCarla fail**");
        wx.showToast({
          title: '获取我的设备失败!',
          icon: 'none',
          duration: 5000
        })
      }
    },
    fail: res => {
      wx.hideToast();
      console.log("********bindCarla fail********");
      console.log(res)
      console.log("*********bindCarla fail*******");
      wx.showToast({
        title: '获取我的设备失败!',
        icon: 'none',
        duration: 5000
      })
    }
  });
}

/**
 * 开机
 */
function openCarla(imei, token, openid) {
  console.log(imei);
  console.log(openid);
  console.log(token);
  wx.showToast({
    title: '正在开机...',
    icon: 'loading'
  });
  wx.request({
    //后台接口地址
    url: config.serverUrl + '/web/api/inner/opr/openCarla',
    data: {
      openid: openid,
      imei: imei,
      token: token,
    },
    method: 'POST',
    header: {
      'content-type': 'application/x-www-form-urlencoded'
    },
    success: function (res) {
      wx.hideToast();
      if (res) {
        var respData = res.data;
        if (respData.code == '000000') {
          wx.navigateTo({
            url: "../carla/carla?qrData=" + encodeURIComponent(imei) + "&infoFlag=false"
          });
          console.log("开启成功!");
        } else if (respData.code == '000003') {
          wx.showToast({
            title: '请退出小程序再登录!',
            icon: 'none',
            duration: 5000
          })
        } else {
          console.log("**openCarla fail**");
          console.log(res)
          console.log("**openCarla fail**");
          wx.showToast({
            title: '开启失败!',
            icon: 'none',
            duration: 5000
          })
        }
      } else {
        console.log("**openCarla fail**");
        console.log(res)
        console.log("**openCarla fail**");
        wx.showToast({
          title: '开启失败!',
          icon: 'none',
          duration: 5000
        })
      }
    },
    fail: res => {
      wx.hideToast();
      console.log("**openCarla fail**");
      console.log(res)
      console.log("**openCarla fail**");
      wx.showToast({
        title: '开启失败!',
        icon: 'none',
        duration: 5000
      })
    }
  });
}

Page({
  data: {
    list: []
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取我的设备列表
    var that = this;
    getMyDeviceList(that);
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    console.log("onShareAppMessage");
    return {
      title: '香氛机',
      path: '/pages/index/index',
      imageUrl: '../../images/share.png'
    }
  },
  setCarla: function (e) {
    //去到管理界面
    var that = this;
    //得到下标
    var index = e.currentTarget.dataset.index;
    var list = that.data.list;
    var deviceInfo = list[index];
    console.log(deviceInfo);
    wx.navigateTo({
      url: "../carla/carla?qrData=" + encodeURIComponent(deviceInfo.imei) +"&infoFlag=true"
    });
  },
  unBindCarla: function (e) {
    //解绑
    var that = this;
    //得到下标
    var index = e.currentTarget.dataset.index;
    var list = that.data.list;
    var deviceInfo = list[index];
    console.log(deviceInfo);

    var imei = deviceInfo.imei;
    var openid = app.globalData.openid;
    var token = app.globalData.token;
    console.log(imei);
    console.log(openid);
    wx.showToast({
      title: '正在解绑...',
      icon: 'loading'
    });

    wx.request({
      //后台接口地址
      url: config.serverUrl + '/web/api/inner/opr/unBindCarla',
      data: {
        openid: openid,
        imei: imei,
        token: token,
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideToast();
        if (res) {
          var respData = res.data;
          if (respData) {
            if (respData.code == '000000') {
              wx.showToast({
                title: '解绑成功!',
                icon: 'none',
                duration: 5000
              });
              //获取我的设备列表
              getMyDeviceList(that);
            } if (respData.code =="000011"){
              wx.showToast({
                title: '设备正在使用，不能解绑!',
                icon: 'none',
                duration: 5000
              });
            }else if (respData.code == '000003') {
              wx.showToast({
                title: '请退出小程序在登录!',
                icon: 'none',
                duration: 5000
              })
            } else {
              console.log("********fail********");
              console.log(res)
              console.log("*********fail*******");
              wx.showToast({
                title: '解绑失败!',
                icon: 'none',
                duration: 5000
              })
            }
          } else {
            console.log("********fail********");
            console.log(res)
            console.log("*********fail*******");
            wx.showToast({
              title: '解绑失败!',
              icon: 'none',
              duration: 5000
            })
          }
        } else {
          console.log("********fail********");
          console.log(res)
          console.log("*********fail*******");
          wx.showToast({
            title: '解绑失败!',
            icon: 'none',
            duration: 5000
          })
        }
      },
      fail: res => {
        console.log("********fail********");
        console.log(res)
        console.log("*********fail*******");
        wx.hideToast();
        wx.showToast({
          title: '解绑失败!',
          icon: 'none',
          duration: 5000
        })
      }
    });

  },
  closeCarla: function (e) {
    //关机
    var that = this;
    //得到下标
    var index = e.currentTarget.dataset.index;
    var list = that.data.list;
    var deviceInfo = list[index];
    console.log(deviceInfo);

    //关机
    var imei = deviceInfo.imei;
    var openid = app.globalData.openid;
    var token = app.globalData.token;
    console.log(imei);
    console.log(openid);
    wx.showToast({
      title: '正在关机...',
      icon: 'loading'
    });
    wx.request({
      //后台接口地址
      url: config.serverUrl + '/web/api/inner/opr/closeCarla',
      data: {
        openid: openid,
        imei: imei,
        token: token,
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideToast();
        if (res) {
          var respData = res.data;
          if (respData) {
            if (respData.code == '000000') {
              wx.showToast({
                title: '关机成功!',
                icon: 'none',
                duration: 5000
              });
              //获取我的设备列表
              getMyDeviceList(that);
            } else if (respData.code == '000003') {
              wx.showToast({
                title: '请退出小程序在登录!',
                icon: 'none',
                duration: 5000
              })
            } else {
              console.log("********fail********");
              console.log(res)
              console.log("*********fail*******");
              wx.showToast({
                title: '关机失败!',
                icon: 'none',
                duration: 5000
              })
            }
          } else {
            console.log("********fail********");
            console.log(res)
            console.log("*********fail*******");
            wx.showToast({
              title: '关机失败!',
              icon: 'none',
              duration: 5000
            })
          }
        } else {
          console.log("********fail********");
          console.log(res)
          console.log("*********fail*******");
          wx.showToast({
            title: '关机失败!',
            icon: 'none',
            duration: 5000
          })
        }
      },
      fail: res => {
        console.log("********fail********");
        console.log(res)
        console.log("*********fail*******");
        wx.hideToast();
        wx.showToast({
          title: '关机失败!',
          icon: 'none',
          duration: 5000
        })
      }
    });
  },
  openCarla : function(e){
    //开机
    var that = this;
    //得到下标
    var index = e.currentTarget.dataset.index;
    var list = that.data.list;
    var deviceInfo = list[index];
    console.log(deviceInfo);

    var imei = deviceInfo.imei;
    var openid = app.globalData.openid;
    var token = app.globalData.token;
    openCarla(imei, token, openid);
  }
})